import { prisma, checkDatabaseConnection } from '../lib/database.js'

async function testConnection() {
  console.log('🔍 Testing database connection...')
  console.log('Database URL:', process.env.DATABASE_URL?.replace(/:[^:@]*@/, ':***@'))

  try {
    const isConnected = await checkDatabaseConnection()

    if (isConnected) {
      console.log('✅ Database connection successful!')

      // 测试基本查询
      console.log('🔍 Testing basic queries...')
      const developerCount = await prisma.developer.count()
      console.log(`📊 Developer count: ${developerCount}`)

      const pluginCount = await prisma.plugin.count()
      console.log(`📊 Plugin count: ${pluginCount}`)

      // 检查插件状态
      console.log('🔍 Checking plugin statuses...')
      const plugins = await prisma.plugin.findMany({
        select: {
          name: true,
          displayName: true,
          status: true,
          featured: true,
          verified: true,
          lastUpdatedAt: true,
          createdAt: true
        }
      })

      console.log('📦 Plugins in database:')
      plugins.forEach(p => {
        console.log(`  - ${p.displayName} (${p.name}): status=${p.status}, featured=${p.featured}, verified=${p.verified}`)
        console.log(`    lastUpdatedAt: ${p.lastUpdatedAt}, createdAt: ${p.createdAt}`)
      })

      // 测试插件查询条件
      console.log('🔍 Testing plugin query conditions...')
      const approvedPlugins = await prisma.plugin.findMany({
        where: { status: 'approved' },
        select: { name: true, displayName: true, status: true }
      })
      console.log(`📊 Approved plugins: ${approvedPlugins.length}`)
      approvedPlugins.forEach(p => {
        console.log(`  - ${p.displayName}: ${p.status}`)
      })

    } else {
      console.log('❌ Database connection failed!')
      process.exit(1)
    }
  } catch (error) {
    console.error('❌ Database connection error:', error)
    process.exit(1)
  } finally {
    await prisma.$disconnect()
  }
}

testConnection()
